<style lang="scss" scoped>
@import '../../style/mixin-rem.scss';
.book-search-history {
  padding: rem(20px) rem(40px) 0 rem(40px);
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header_title {
    font-size: rem(18px);
    color: #42464e;
  }
  .iconfont {
    font-size: rem(16px);
    color: #bbb;
  }
  .content {
    padding-top: rem(22px);
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  .content_item {
    font-size: rem(18px);
    color: #bbb;
    margin-right: rem(20px);
    margin-bottom: rem(20px);
  }
}
</style>

<template>
  <div class="book-search-history" v-show="searchResults === null || searchResults.length === 0">
    <div class="header">
      <div class="header_title">历史记录</div>
      <div class="btn-delete">
        <div class="iconfont icon-lajitong" @click="clearHistory"></div>
      </div>
    </div>
    <div class="content">
      <div class="content_item" v-for="(item, index) of historyRecord" :key="item.id">
        <span v-text="item" @click="getSearchResults(index)"></span>
      </div>
      <div class="content_item" v-show="historyRecord.length === 0">
        历史记录为空?试试搜索一下吧~~
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BookSearchHistory',
  created () {
    this.$store.commit('bookSearch/initHistory')
  },
  computed: {
    searchResults () {
      return this.$store.state.bookSearch.searchResults
    },
    historyRecord () {
      return this.$store.state.bookSearch.history
    }
  },
  methods: {
    clearHistory () {
      this.$store.commit('bookSearch/clearHistory')
    },
    getSearchResults (index) {
      this.$store.dispatch('bookSearch/getSearchResults',
        this.historyRecord[index])
    }
  }
}
</script>
